package com.hung.triple.bootstrap;

import com.google.common.collect.Lists;
import com.hung.triple.bootstrap.config.AbstractTripleGlobalConfig;
import com.hung.triple.bootstrap.config.TripleRegistryConfig;
import com.hung.triple.common.enums.TripleRegisterType;
import com.hung.triple.common.enums.TripleRemoteProtocol;
import com.hung.triple.common.enums.TripleSerializerType;
import com.hung.triple.loadbalancer.RandomLoadBalancer;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Hung
 * @date 2022/9/24 15:14
 */
@Configuration(proxyBeanMethods = false)
public class TripleProviderConfig extends AbstractTripleGlobalConfig {


    /**
     * 权重
     */
    @Setter
    @Getter
    protected static Integer weight;

    @Bean("tripleProviderBootstrap")
    public TripleProviderBootstrap initTriple() throws UnknownHostException {

        // 1. 全局配置
        TripleProviderConfig.setProtocol(TripleRemoteProtocol.TRIPLE_NETTY);
        TripleProviderConfig.setTimeoutMS(2000);
        TripleProviderConfig.setSerializerType(TripleSerializerType.PROTOBUF);
        TripleProviderConfig.setLoadBalancer(RandomLoadBalancer.class);
        TripleProviderConfig.setWeight(100);
        TripleProviderConfig.setLocalIp(InetAddress.getLocalHost().getHostAddress());
        TripleProviderConfig.setLocalPort("9876");

        // 2. 注册中心配置
        TripleRegistryConfig.setRegisterType(TripleRegisterType.NACOS);
        TripleRegistryConfig.setAddressList(Lists.newArrayList("192.168.119.130:8848"));
        TripleRegistryConfig.setTimeoutMS(10000);

        // 3. 创建服务器
        TripleProviderBootstrap server = new TripleProviderBootstrap();

        return server;
    }

}